<?php
declare (strict_types=1);

namespace app\topic\service\admin;

use app\common\logic\category\admin\Category;
use app\common\service\BaseService;
use app\topic\logic\admin\Topic as TopicLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;
use think\Response;

/**
 * 资讯管理
 */
class Topic extends BaseService
{

    /**
     * @Inject()
     *
     * @var TopicLogic
     */
    protected $topicLogic;

    /**
     * 列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     */
    public function index()
    {
        // 搜索功能
        $map = [];
        // Request::has('mhm_id', 'get', true) && $map[] = ['mhm_id', '=', Request::get('mhm_id/d')];
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $category = Request::get('category/d', 0);
        $category && $map[] = ['category', 'like', '%,' . $category . ',%'];
        $data        = $this->topicLogic->getList($map);
        $is_platform = request()->is_platform;
        // 定义字段
        $keyList = [
            'title'       => ListItem::make('custom')->title('标题'),
            // 'school_name' => ListItem::make()->title('机构')->visible($is_platform),
            'read_count'  => ListItem::make()->title('浏览量'),
            'source'      => ListItem::make()->title('来源'),
            'update_time' => ListItem::make()->title('最后更新时间'),
        ];

        // 资讯分类
        $category  = app(Category::class)->cascaderCate(0, 5);
        $searchKey = [
            // 'mhm_id'   => FormItem::make('searchSchool')->title(false)->visible($is_platform),
            'title'    => FormItem::make()->title(false)->placeholder('名称')->maxlength(30),
            'category' => FormItem::make('cascader')->title(false)->options($category)->placeholder('分类')->required(),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->datas     = $data;
        $list->searchKey = $searchKey;

        return $list->send();
    }

    /**
     * 新建
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     */
    public function create(): Response
    {
        if (Request::isPost()) {
            $data = Request::post();
            $res  = $this->topicLogic->createTopic($data);

            return static::parseToData($res->toArray(), 1, '添加成功');
        }

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $this->keyListInit();

        $form->saveMethod  = 'POST';
        $form->savePostUrl = '/topic/admin/create';

        return $form->send();
    }

    /**
     * 编辑
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     *
     * @param int $id [description]
     */
    public function update(int $id): Response
    {
        if (Request::isPut()) {
            $data = Request::put();
            $res  = $this->topicLogic->editTopic($id, $data);

            return static::parseToData($res->toArray(), 1, '编辑成功');
        }

        $data = $this->topicLogic->with(['coverUrl'])->where('id', $id)->findOrFail();

        $data->cates = $data->fullcategorypath;

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $this->keyListInit();

        $form->datas = $data;

        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/topic/admin/' . $id . '/edit';

        return $form->send();
    }

    /**
     * 删除
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     *
     * @param int $id [description]
     */
    public function delete(int $id): Response
    {
        $this->topicLogic->deleteTopic($id);

        return static::parseToData([], 1, '删除成功');
    }

    /**
     * 初始化字段
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     */
    protected function keyListInit(): array
    {
        // 机构分类
        $category = app(Category::class)->cascaderCate(0, 5);

        return [
            'title'    => FormItem::make()->title('名称')->maxlength(100)->required(),
            'cates'    => FormItem::make('cascader')->title('分类')->options($category)->multiple(true)->required(),
            'cover'    => FormItem::make('custom')->title('封面图')->required()->help('建议尺寸 221px*122Px'),
            'abstract' => FormItem::make('textarea')->title('摘要')->rows(4)->maxlength(120)->placeholder('可以输入120字的摘要')->required(),
            'content'  => FormItem::make('editor')->title('内容')->required(),
            // 'best_sort' => FormItem::make('inputNumber')->title('排序值'),
            'source'   => FormItem::make()->title('来自')->help('资讯内容出处'),

        ];
    }

    /**
     * 编辑某个字段
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/11/18
     *
     * @param int    $id
     * @param string $field
     * @return Response
     */
    public function editField(int $id, string $field): Response
    {
        $value = Request::put($field);
        $this->topicLogic->editField($id, $field, $value);

        return static::parseToData([], 1, '编辑成功');
    }

    /**
     * 获取资讯选择列表
     *
     * @return \eduline\admin\page\interfaces\think\Response
     */
    public function getTopicList()
    {
        $map = [];

        // 条件
        $category = Request::get('category/d', 0);
        $category && $map[] = ['category', 'like', '%,' . $category . ',%'];

        $keyword = Request::get('title/s');

        // 数量
        $count = Request::get('count/d', 15);

        $data = $this->topicLogic->getTopicList($keyword, $count, $map);

        $list        = new PageList();
        $list->datas = $data;

        return $list->send();
    }

    /**
     * 获取资讯信息
     *
     * @return \think\Response
     */
    public function getTopics()
    {
        /** @var array $ids */
        $ids = Request::get('ids/a');

        $datas = $this->topicLogic->getTopics($ids);

        return $this->parseToData($datas, 1, '获取成功');
    }

    /**
     * 批量删除
     *
     * @param $ids
     * @return \think\Response
     */
    public function deletes($ids)
    {
        $this->topicLogic->deletes($ids);

        return $this->parseToData([], 1, '操作成功');
    }
}
